package com.ohuang.util_a.task;

import java.util.ArrayList;
import java.util.List;

public class TaskRunUtil {


    public static void  stopTasks(List<Task> tasks){
        if (tasks!=null&&tasks.size()>0){
            for (Task task : tasks) {
                task.setStop(true);
            }
        }
    }

    public static void  stopTask(Task task){
        if (task!=null){
            task.setStop(true);
        }
    }

    /**
     *  执行任务
     * @param task
     * @param taskCallBack
     */
    public static void runTask(Task task,TaskCallBack taskCallBack){
        if (task!=null){
            task.startRun(new OnceTaskCallBack() {
                @Override
                public void onCall() {
                    task.setStop(true);
                    taskCallBack.onNext();
                }
            });
        }
    }

    /**
     *  并发调用  Task全部完成后 通过TaskCallBack.onNext();回调
     * @param tasks
     * @param taskCallBack
     */
    public static void runTaskList(List<Task> tasks,TaskCallBack taskCallBack){
        if (tasks!=null&&tasks.size()>0){
            int size = tasks.size();
            TaskCallBack taskCallBackMy=new TaskCallBack() {
                int count=0;
                @Override
                public void onNext() {
                    count++;
                    if (count>=size){
                        taskCallBack.onNext();
                    }
                }
            };
            for (int i1 = 0; i1 < tasks.size(); i1++) {
                int finalI = i1;
                tasks.get(i1).startRun(new OnceTaskCallBack() {
                    @Override
                    public void onCall() {
                        tasks.get(finalI).setStop(true);
                        taskCallBackMy.onNext();
                    }
                });
            }
        }else {
            taskCallBack.onNext();
        }
    }

    /**
     * 链式调用 所用Task完成后 通过TaskCallBack.onNext();回调
     */
    public static void runTaskForLink(List<Task> tasks,TaskCallBack taskCallBack){
        if (tasks!=null&&tasks.size()>0){
            int size = tasks.size();
            List<TaskCallBack> taskCallBacks=new ArrayList<>();
            TaskCallBack taskCallBackMy=new TaskCallBack() {
                int count=0;
                @Override
                public void onNext() {
                    count++;
                    if (count>=size){
                        taskCallBack.onNext();
                    }else {
                        tasks.get(count).startRun(taskCallBacks.get(count));
                    }
                }
            };
            for (int i = 0; i < tasks.size(); i++) {
                int finalI = i;
                taskCallBacks.add(new OnceTaskCallBack() {
                    @Override
                    public void onCall() {
                        tasks.get(finalI).setStop(true);
                        taskCallBackMy.onNext();
                    }
                });
            }
            tasks.get(0).startRun(taskCallBacks.get(0));
        }else {
            taskCallBack.onNext();
        }


    }


}
